#!/usr/bin/env python
# @Software: PyCharm
# @Time : 2022/8/4 17:19
# @Author : v_nahuachen
# @Version：V 0.1
# @File : base_def.py
# @desc :


class BaseDef:
    """
    封装Minium的页面跳转、元素定位等方法
    """

    def __init__(self, mini):
        """
        基类初始化 Minium 实例
        """
        self.mini = mini

    def navigate_to_page(self, route):
        """
        跳转到指定页面
        """
        self.mini.app.navigate_to(route)
        ret = self.wait_page(route)
        return ret

    def redirect_to_page(self, route):
        """
        跳转到指定页面并关闭当前页面
        """
        self.mini.app.redirect_to(route)
        ret = self.wait_page(route)
        return ret

    def switch_to_tabbar(self, route):
        """
        跳转到tabbar页面，关闭其他非tabbar页面
        """
        self.mini.app.switch_tab(route)
        ret = self.wait_page(route)
        return ret

    def wait_page(self, route):
        """
        等待页面跳转成功
        """
        ret = self.mini.app.wait_for_page(route)
        if ret:
            return True
        return False

    def get_element_selector(self, selector, timeout=10):
        boo = self.mini.page.element_is_exists(selector, max_timeout=timeout)
        ret = None
        if boo:
            ret = self.mini.page.get_element(selector)
        return ret

    def get_element_selector_text(self, selector, inner_text, timeout=10):
        boo = self.mini.page.element_is_exists(selector, max_timeout=timeout)
        ret = None
        if boo:
            ret = self.mini.page.get_element(selector, inner_text)
        return ret

    def get_elements_selector(self, selector, timeout=10):
        boo = self.mini.page.element_is_exists(selector, max_timeout=timeout)
        ret = None
        if boo:
            ret = self.mini.page.get_elements(selector)
        return ret

    def get_elements_selector_text(self, selector, inner_text, timeout=10):
        boo = self.mini.page.element_is_exists(selector, max_timeout=timeout)
        ret = None
        if boo:
            ret = self.mini.page.get_elements(selector, inner_text)
        return ret

    def get_element_xpath(self, xpath, timeout=10):
        boo = self.mini.page.element_is_exists(xpath, max_timeout=timeout)
        ret = None
        if boo:
            ret = self.mini.page.get_element_by_xpath(xpath)
        return ret

    def get_element_custom(self, custom, timeout=10):
        boo = self.mini.page.element_is_exists(custom, max_timeout=timeout)
        ret = None
        if boo:
            ret = self.mini.page.get_element(custom)
        return ret

